﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using MySql.Data.MySqlClient;

namespace TestWebService
{
    /// <summary>
    /// MyService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class MyService : System.Web.Services.WebService
    {
        [WebMethod]
        public string[] getName(string Idnumber)
        {
            MySqlConnection conn = null;
            //string[] info;
            List<string> information = new List<string>();
            information.Add(Idnumber);
            try
            {
                string connstr = "Server=127.0.0.1;Database=mysql;Uid=root;Pwd=mysql";
                conn = new MySqlConnection(connstr);
                float U, I, P, KWh;
                DateTime datetime;
                string message;
                string query = "select U,I,P,KWh,Time from socialgame.socialgame where Idnumber="+Idnumber;
                MySqlCommand cmd2 = new MySqlCommand(query, conn);
                conn.Open();
                MySqlDataReader reader = cmd2.ExecuteReader();
                while (reader.Read())
                {
                    U = float.Parse(reader["U"].ToString());
                    I = float.Parse(reader["I"].ToString());
                    P = float.Parse(reader["P"].ToString());
                    KWh = float.Parse(reader["KWh"].ToString());
                    datetime = (DateTime)reader["Time"];
                    message = "U:" + U.ToString() + "; I:" + I.ToString() + "; P:" + P.ToString() + "; KWh:" + KWh.ToString() + "; Time:" + datetime.ToString();
                    information.Add(message);
                    //Console.WriteLine("Idnumber:{0}; U:{1}; I:{2}; P:{3}; KWh:{4}; Time:{5}", Idnumber, U, I, P, KWh, datetime);
                }
                conn.Close();
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }
            //information.Add("Idnumber:" + Idnumber + "\n");
            return information.ToArray<string>();
        }
        [WebMethod]
        public string getAge()
        {
            return "25";
        }
    }
}